﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;


namespace H2O
{

    class SimpleWebRequest
    {
        // static string content = null;
        public ManualResetEvent allDone = new ManualResetEvent(false);
        public string send(string url)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E H2O)";
                SimpleRequestState requestState = new SimpleRequestState(request);
                request.BeginGetResponse(new AsyncCallback(OnResponse), requestState);
                allDone.WaitOne();
                return requestState.RequestData.ToString();
            }
            catch (Exception)
            {
            }
            return "";

        }

        void OnResponse(IAsyncResult ar)
        {
            try
            {
                SimpleRequestState requestState = (SimpleRequestState)ar.AsyncState;
                HttpWebRequest request = (HttpWebRequest)requestState.Request;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);

                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                requestState.RequestData.Append(sr.ReadToEnd());

            }
            catch (Exception)
            {

            }
            finally
            {
                allDone.Set();
            } 
        }
    }
    public class SimpleRequestState
    {

        public StringBuilder RequestData;

        public HttpWebRequest Request;

        public SimpleRequestState()
        {
            RequestData = new StringBuilder(String.Empty);
            Request = null;
        }
        public SimpleRequestState(HttpWebRequest req)
        {
            RequestData = new StringBuilder(String.Empty);
            Request = req;
        }
    }
}
